/*
 * @Author: wangdazhuang
 * @Date: 2024-10-19 09:25:19
 * @LastEditTime: 2025-02-18 11:31:05
 * @LastEditors: wangdazhuang
 * @Description: 
 * @FilePath: /pornhub_app/lib/http/http_error_interceptor.dart
 */

import 'package:dio/dio.dart';

import 'http_const.dart';
import 'http_special_code_handler.dart';

///特殊请求错误拦截器
abstract class HttpErrorInterceptor {
  static const magicCode = -12345;
  static void Function(DioException, ErrorInterceptorHandler)? onError =
      (error, handler) {
    final e = error.error;
    final isspecial = error.response == null &&
        error.message == null &&
        e != null &&
        e is StateError;
    if (isspecial) {
      final flag =
          e.message.contains('Response has no Location header for redirect');
      if (flag) {
        HttpSpecialCodeHandler.doAction(
          Response(
            requestOptions: error.requestOptions,
            data: {
              "code": 301,
              "msg": '请杀死App后重新打开！',
            },
          ),
        );
      }
    }
    final resp = error.response;
    if (resp == null || resp.data == null) {
      handler.reject(error);
      return;
    }
    final data = resp.data;
    final code = data["code"] ?? magicCode;

    ///处理特殊业务码
    if (HttpConst.specialCodeList.contains(code)) {
      HttpSpecialCodeHandler.doAction(resp);
      return;
    }
    handler.reject(error);
    return;
  };
}
